using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GridDataBound2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;//ConnectionString
            con.Open();
            int page_size = 10;//default page size
            int offset = 1;//default page
            string sortCol, sortDir;
            if (string.IsNullOrEmpty(Request["sort"]))
            {
                sortCol = "ID_MOVIE";
                sortDir = "ASC";
            }
            else
            {
                sortCol = Request["sort"];
                sortDir = Request["dir"];
            }
            if (!string.IsNullOrEmpty(Request["limit"]))
            {
                page_size = int.Parse(Request["limit"]);
                offset = int.Parse(Request["start"]);
            }


            /*
             * if you use the PagingToolbar the start parameter 
             * is not refering to the start page but to the start record
            */

            offset = offset / page_size;

            offset += 1;
            //query that gets only the records needed to the page
            // using the new ROW_NUMBER function in sql2005
            string sql = "WITH MOVIES AS ( " +
                        " SELECT ROW_NUMBER() OVER " +
                        "(ORDER BY " + sortCol + " " + sortDir + ")AS Row," +
                        " ID_MOVIE,TITLE, RATING,VOTES,YEAR,GENRE " +
                        " FROM TB_MOVIE )" +
                        " SELECT ID_MOVIE,TITLE, GENRE,RATING,VOTES,YEAR" +
                        " FROM MOVIES " +
                        " WHERE Row between (@PageIndex-1)* @PageSize+1  and @PageIndex*@PageSize";

            SqlCommand cmd = new SqlCommand(sql, con);
            //add the parameters to the query to grab the correct page
            cmd.Parameters.AddWithValue("@PageIndex", offset);
            cmd.Parameters.AddWithValue("@PageSize", page_size);
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adapt.Fill(ds);
            //closes the objects and disposes

            //GetMovieCount(ds);
            con.Close();
            cmd.Dispose();
            con.Dispose();
            YuiGrid1.TotalRecords = GetMovieCount();
            YuiGrid1.DataSource = ds;
            YuiGrid1.DataBind();
        }

    }
    private int GetMovieCount()
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;//ConnectionString
        con.Open();
        string sql = "SELECT COUNT(*) FROM TB_MOVIE";
        SqlCommand cmd = new SqlCommand(sql, con);

        int cont = int.Parse(cmd.ExecuteScalar().ToString());
        cmd.Dispose();
        con.Dispose();
        return cont;
    }
}
